const axios = function(options={}){
    let {url,method="get",data,params={}} = options;
    let paramsArr = Object.keys(params);
    if(paramsArr.length>0){
        url+="?"+paramsArr.map(key=>key+"="+params[key]).join("&")
    }
    let body;
    let headers = {};
    if(method === "post"){
        body =data
        headers["Content-Type"]="application/x-www-form-urlencoded";
        if(data && typeof data === "object"){
            body = JSON.stringify(data);
            headers["Content-Type"]="application/json";
        }
    }

    return fetch(url,{
        method,
        body,
        headers
    }).then(value=>value.json())
}
axios.get = function(url,options={}){
    return this({
        url,
        ...options
    });
}
axios.post = function(url,data,options={}){
    return this({
        url,
        method:"post",
        data,
        ...options
    })
}
export default axios;